package com.how2java;

import com.how2java.pojo.Category;
import com.how2java.pojo.Order;
import com.how2java.pojo.OrderItem;
import com.how2java.pojo.Product;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by WheelChen on 2017/4/19.
 */
public class TestMybatis {
    public static void main(String[] args) throws IOException {
//      根据配置文件mybatis-config.xml得到sqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
//      然后再根据sqlSessionFactory 得到session
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session=sqlSessionFactory.openSession();
//      增加
//        Category c = new Category();
//        c.setName("新增加的Category");
//        session.insert("addCategory",c);
//
//        listAll(session);
//
////      删除
//        Category c2 = new Category();
//        c2.setId(6);
//        session.delete("deleteCategory",c2);
//
//        listAll(session);
//
////        查找
//        Category c3= session.selectOne("getCategory",3);
//
//        System.out.println(c3.getName());
//
////        修改
//        Category c4= session.selectOne("getCategory",3);
//        c4.setName("修改了的Category名稱");
//        session.update("updateCategory",c4);
//
//        listAll(session);


//      模糊查找
//        List<Category> cs = session.selectList("listCategoryByName","新增加");
//        for (Category c : cs) {
//            System.out.println(c.getName());
//        }

//      多条件查询
//        Map<String, Object> params = new HashMap<>();
//        params.put("id", 3);
//        params.put("name", "cat");
//        List<Category> cs = session.selectList("listCategoryByIdAndName",params);
//        for (Category c : cs) {
//            System.out.println(c.getName());
//        }
//      一对多
//        List<Category> cs = session.selectList("listCategory");
//        for (Category c : cs) {
//            System.out.println(c);
//            List<Product> ps = c.getProducts();
//            for (Product p : ps) {
//                System.out.println("\t"+p);
//            }
//        }

        //多对一
//        List<Product> products = session.selectList("listProduct");
//        for (Product p: products){
//            System.out.println(p + "对应 \t" + p.getCategory());
//        }

//        Category c1 = session.selectOne("getCategory", 1);
//        System.out.println(c1.getProducts());
//        addOrderItem(session);
//        detelOrderItem(session);
//        listOrder(session);

        System.out.println("查询所有的");
        List<Product> ps = session.selectList("listProduct");
        for (Product p : ps) {
            System.out.println(p);
        }

        System.out.println("模糊查询");
        Map<String,Object> params = new HashMap<>();
        params.put("name","a");
        List<Product> ps2 = session.selectList("listProductByName",params);
        for (Product p : ps2) {
            System.out.println(p);
        }

        session.commit();
        session.close();

    }



    //查询所有分类
    private static void listAll(SqlSession session) {
        List<Category> cs = session.selectList("listCategory");
        for (Category c : cs) {
            System.out.println(c.getName());
        }
    }
    //查询所有订单
    private static void listOrder(SqlSession session) {
        List<Order> os = session.selectList("listOrder");
        for (Order o : os) {
            System.out.println(o.getCode());
            List<OrderItem> ois= o.getOrderItems();
            for (OrderItem oi : ois) {
                System.out.format("\t%s\t%f\t%d%n", oi.getProduct().getName(),oi.getProduct().getPrice(),oi.getNumber());
            }
        }
    }
    //增加单条订单中的订单项
    private static void addOrderItem(SqlSession session){
        //获得订单
        Order order= session.selectOne("getOrder", 1);
        //获得具体产品
        Product product = session.selectOne("getProduct", 6);

        OrderItem oi = new OrderItem();
        oi.setOrder(order);
        oi.setProduct(product);
        oi.setNumber(200);
        session.insert("addOrderItem", oi);
    }
    //删除订单中的订单项
    private static void detelOrderItem(SqlSession session){
        //获得订单
        Order order= session.selectOne("getOrder", 1);
        //获得具体产品
        Product product = session.selectOne("getProduct", 6);

        OrderItem oi = new OrderItem();
        oi.setOrder(order);
        oi.setProduct(product);
        session.delete("deleteOrderItem", oi);
    }

}
